import {join} from 'path'
const shell = require('electron').shell
const spawn = require('child_process').spawn
const cliPath=join($dirname, '../cli.exe')
export async function send (command,params) {
	console.log(command)
	var c=command.split("/")

	var str=JSON.stringify(params)

	console.log(str)
	var cli=spawn(cliPath,[c[0],c[1],str])
    return new Promise((resolve, reject) => {
	    cli.stdout.on('data', function (data) {
	    		console.log(data+"")
	        // resolve(JSON.parse(JSON.stringify(data)))
	        resolve(JSON.parse(data+""))
	    });
	    cli.stderr.on('data', function (data) {
	    	console.log(data+"")
	    	var m={
	    		"errcode":0,
	    		"errmsg":data+""
	    	}
	        resolve(m)
	    });
	    cli.on('exit', function (code) {
	    	console.log(code+"")
	    	var m = {
	    		"errcode":0,
	    		"errmsg":"程序出错了"
	    	}
	    });
	})
}

export async function upload () {
	setInterval(function(){

			/*var cli=spawn(cliPath,['upload'])
			cli.stdout.on('data', function (data) {
	    	console.log(data+"")
	       
	    });
	    cli.stderr.on('data', function (data) {
	    	console.log(data+"")
	    	
	    });
	    cli.on('exit', function (code) {
	    	console.log(code+"")
	    });*/
	},10000)
}

export async function openLink(url){
		shell.openExternal(url)
}

